﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Route_Hotel_Entity : EntityBase<ROUTE_HOTEL>
    {
        public override RESP_Info Add(ROUTE_HOTEL model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into ROUTE_HOTEL(ROUTE_ID,HOTE_ID) values (@ROUTE_ID,@HOTE_ID)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", model.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("HOTE_ID", model.HOTE_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加旅程酒店成功</p></h5>";
                    info.Url = "RouteHotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加旅程酒店失败</p></h5>";
                    info.Url = "RouteHotelAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加旅程酒店失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(ROUTE_HOTEL model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update ROUTE_HOTEL set HOTE_ID=@HOTE_ID where ROUTE_ID=@ROUTE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", model.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("HOTE_ID", model.HOTE_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑旅程酒店成功</p></h5>";
                    info.Url = "RouteHotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑旅程酒店失败</p></h5>";
                    info.Url = "RouteHotelAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑旅程酒店失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(ROUTE_HOTEL routeHotel)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from ROUTE_HOTEL where ROUTE_ID=@ROUTE_ID AND HOTE_ID=@HOTE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", routeHotel.ROUTE_ID));
                    cmd.Parameters.Add(new SqlParameter("HOTE_ID", routeHotel.HOTE_ID));

                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除旅程酒店成功</p></h5>";
                    info.Url = "RouteHotelList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除旅程酒店失败</p></h5>";
                    info.Url = "RouteHotelList.aspx";

                    Tom.Net.Log.LogManager.Error("删除旅程酒店失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override ROUTE_HOTEL GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select ROUTE_ID,HOTE_ID from ROUTE_HOTEL where ROUTE_ID=@ROUTE_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("ROUTE_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ROUTE_HOTEL routeHotel = new ROUTE_HOTEL();
                        if (!reader.IsDBNull(0)) routeHotel.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeHotel.HOTE_ID = reader.GetString(1);
                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routeHotel;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程酒店数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override ROUTE_HOTEL GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        ROUTE_HOTEL routeHotel = new ROUTE_HOTEL();
                        if (!reader.IsDBNull(0)) routeHotel.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeHotel.HOTE_ID = reader.GetString(1);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return routeHotel;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程酒店数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<ROUTE_HOTEL> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<ROUTE_HOTEL> routeHotels = new List<ROUTE_HOTEL>();
                    while (reader.Read())
                    {
                        ROUTE_HOTEL routeHotel = new ROUTE_HOTEL();
                        if (!reader.IsDBNull(0)) routeHotel.ROUTE_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) routeHotel.HOTE_ID = reader.GetString(1);

                        routeHotels.Add(routeHotel);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return routeHotels;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取旅程酒店数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}

